//
//  RGFocusVedioListViewModel.m
//  RedGrass
//
//  Created by wukong on 2019/1/27.
//  Copyright © 2019年 hongcaosp. All rights reserved.
//

#import "RGFocusVedioListViewModel.h"
#import "HttpRequest.h"


#define pageSize 10

@interface RGFocusVedioListViewModel()

@property (weak, nonatomic)   id<RGFocusVedioListViewProtocol> protocol;
//关注视频列表
//@property (strong, nonatomic) NSMutableArray<RGRecommendVideoModel *> *videoList; //关注视频列表
//@property (assign, nonatomic) NSInteger pagaNum;
//@property (assign, nonatomic) NSInteger total;

@end

@implementation RGFocusVedioListViewModel

-(instancetype) initWithProtocol:(id<RGFocusVedioListViewProtocol>)protocol{
    if(self = [super init]){
        self.protocol = protocol;
        self.pagaNum = 1;
    }
    return self;
}


//获取推荐视频列表
-(void) gainFocusVideoListWithIsFirstLoad:(BOOL)isfristLoad isLoadMore:(BOOL)isLoadMore{
  
    if(isfristLoad){
         [self.protocol showLoading:@""];
        self.pagaNum = 1;
    }
    if(isLoadMore){
        self.pagaNum ++;
    }else{
        self.pagaNum = 1;
    }
    NSString *token = [RGUserInfoModel getUserInfo].token;
    NSDictionary *dic = @{@"pageNumber":@(self.pagaNum),
                          @"token":SafeStr(token),
                          @"pageSize":@(pageSize)
                          };
    AA_WEAK_MAKE(self) weakSelf = self;
    HttpRequest *quest = [HttpRequest buildRequest:@"/api/userFans/fansDynamic"];
    [quest post:dic success:^(id data) {
         if(isfristLoad){
             [weakSelf.protocol dismissLoading];
         }
        //        [weakSelf.protocol showTextMessageWithMessage:@"成功"];
        NSArray *originalArr = [data objectForKey:@"list"];
        NSError *error;
        if(originalArr.count){
            NSMutableArray *tempArr = [RGRecommendVideoModel arrayOfModelsFromDictionaries:originalArr error:&error];
            if(tempArr.count){
                if(isfristLoad || !isLoadMore){
                    weakSelf.videoList = tempArr;
                }else{
                    if(weakSelf.videoList == nil){
                        weakSelf.videoList = [NSMutableArray array];
                    }
                    [weakSelf.videoList addObjectsFromArray:tempArr];
                }
            }
        }
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if([weakSelf.videoList count]){
                if([weakSelf.protocol respondsToSelector:@selector(reloadFocusViewWithFocusList:)]){
                    [weakSelf.protocol reloadFocusViewWithFocusList:weakSelf.videoList];
                }
            }else{
                if([weakSelf.protocol respondsToSelector:@selector(reloadFocusViewWithFocusList:)]){
                     [weakSelf.protocol reloadFocusViewWithFocusList:[NSMutableArray array]];
                }
            }
            
            NSInteger total = [[data objectForKey:@"total"] integerValue];
            if(!isfristLoad){
                if(isLoadMore){
                    if([weakSelf.protocol respondsToSelector:@selector(focusMjFooterEndLoading)]){
                        [weakSelf.protocol focusMjFooterEndLoading];
                    }
                   
                }else{
                    if([weakSelf.protocol respondsToSelector:@selector(focusMjHeaderEndLoading)]){
                        [weakSelf.protocol focusMjHeaderEndLoading];
                    }
                    if([weakSelf.protocol respondsToSelector:@selector(playWithFirstIndexWhenLoadSuccess)]){
                        [weakSelf.protocol playWithFirstIndexWhenLoadSuccess];
                    }
                }
            }else{
                if([weakSelf.protocol respondsToSelector:@selector(playWithFirstIndexWhenLoadSuccess)]){
                    [weakSelf.protocol playWithFirstIndexWhenLoadSuccess];
                }
            }
            if(originalArr.count < pageSize){
                if([weakSelf.protocol respondsToSelector:@selector(focusCommentMjFooterStatusWithNoMoreData)]){
                    [weakSelf.protocol focusCommentMjFooterStatusWithNoMoreData];
                }
            }
//            if([weakSelf.protocol respondsToSelector:@selector(playWithFirstIndexWhenLoadSuccess)]){
//                [weakSelf.protocol playWithFirstIndexWhenLoadSuccess];
//            }
        });
//        if(!isLoadMore){
//            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                if([weakSelf.protocol respondsToSelector:@selector(playWithFirstIndexWhenLoadSuccess)]){
//                    [weakSelf.protocol playWithFirstIndexWhenLoadSuccess];
//                }
//
//            });
//        }
     
    } fail:^(NSInteger code, NSString *message) {
        [weakSelf.protocol showTextMessageWithMessage:message];
        if(!isfristLoad){
            if(isLoadMore){
                if([weakSelf.protocol respondsToSelector:@selector(focusMjFooterEndLoading)]){
                    [weakSelf.protocol focusMjFooterEndLoading];
                }
                
            }else{
                if([weakSelf.protocol respondsToSelector:@selector(focusMjHeaderEndLoading)]){
                    [weakSelf.protocol focusMjHeaderEndLoading];
                }
            }
        }
    }];
}


////点赞
//-(void) changeFavoriteStautswithChosedIndexPath:(NSIndexPath *)path{
//    if([self.videoList count] == 0 || path.row >= self.videoList.count){
//        return;
//    }
//    RGRecommendVideoModel *model = self.videoList[path.row];
//    NSString *token = [RGUserInfoModel getUserInfo].token;
//    NSInteger vedioId = model.ID;
//    NSDictionary *dic = @{@"id":@(vedioId),
//                          @"token":SafeStr(token)
//                          };
//    if(model.isLike){//当前状态是已点赞
//        [self.protocol showLoading:@"正在取消点赞"];
//        AA_WEAK_MAKE(self) weakSelf = self;
//        HttpRequest *quest = [HttpRequest buildRequest:@"/api/userLike/cancel"];
//        [quest post:dic success:^(id data) {
//            [weakSelf.protocol dismissLoading];
//            [weakSelf.protocol showTextMessageWithMessage:@"成功取消点赞"];
//            model.isLike = NO;
//            model.likeCounts = ((model.likeCounts - 1) >= 0)? (model.likeCounts - 1):0;
//            if([weakSelf.protocol respondsToSelector:@selector(reloadFocusViewCellWithIndexPath:)]){
//                [weakSelf.protocol reloadFocusViewCellWithIndexPath:path];
//            }
//        } fail:^(NSInteger code, NSString *message) {
//            
//            [weakSelf.protocol showTextMessageWithMessage:message];
//        }];
//    }else{
//        [self.protocol showLoading:@"正在点赞"];
//        AA_WEAK_MAKE(self) weakSelf = self;
//        HttpRequest *quest = [HttpRequest buildRequest:@"/api/userLike/add"];
//        [quest post:dic success:^(id data) {
//            [weakSelf.protocol dismissLoading];
//            [weakSelf.protocol showTextMessageWithMessage:@"成功点赞"];
//            model.isLike = YES;
//            model.likeCounts = model.likeCounts + 1;
//            if([weakSelf.protocol respondsToSelector:@selector(reloadFocusViewCellWithIndexPath:)]){
//                [weakSelf.protocol reloadFocusViewCellWithIndexPath:path];
//            }
//        } fail:^(NSInteger code, NSString *message) {
//            
//            [weakSelf.protocol showTextMessageWithMessage:message];
//        }];
//    }
//}

@end
